package com.quanshu.shop.goods.view;

import android.content.Context;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.shop.base.tool.BaseTool;
import com.quanshu.shop.base.view.BaseTitleBar;
import com.quanshu.shop.goods.adapter.GoodsListAdapter;
import com.quanshu.shop.goods.entity.GoodsListEntity;
import com.quanshu.shop.main.api.ShopMainIF;
import com.scwang.smartrefresh.header.WaterDropHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GoodsListActivity extends BaseActivity {

    @BindView(R.id.goods_list_title)
    BaseTitleBar goodsListTitle;
    @BindView(R.id.goods_list_sort)
    LinearLayout goodsListSort;
    @BindView(R.id.goods_list_rl)
    RecyclerView goodsListRl;
    @BindView(R.id.goods_list_refresh)
    SmartRefreshLayout goodsListRefresh;
    @BindView(R.id.line_center)
    View lineCenter;
    @BindView(R.id.sort_center)
    ConstraintLayout sortCenter;
    @BindView(R.id.sort_img)
    ImageView sortImg;
    @BindView(R.id.sort_txt)
    TextView sortTxt;
    @BindView(R.id.sort_left)
    TextView sortLeft;
    @BindView(R.id.sort_right)
    TextView sortRight;
    private String title;
    private String categoryId;
    private GoodsListAdapter goodsListAdapter;
    private List<GoodsListEntity.DataEntity> dataEntityList;
    private Context mContext;
    private int sortState = 0;
    private String sortType = null;
    private String sortSort = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_list);
        ButterKnife.bind(this);
        mContext = this;
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    private void initView() {
        RxViewHelper.getInstance().clicks(sortLeft, aVoid ->
                {
                    clearSortState();
                    sortLeft.setEnabled(false);
                    sortState = 0;
                    setMoneySort(sortState);
                    //获取接口
                    initData();
                    sortLeft.setTextColor(getResources().getColor(R.color.color_eb2624));
                }

        );
        RxViewHelper.getInstance().clicks(sortCenter, aVoid ->
                {
                    if (sortState == 0) {
                        clearSortState();
                    }
                    if (sortState >= 2) {
                        sortState = 1;
                    } else {
                        sortState++;
                    }
                    setMoneySort(sortState);
                }

        );
        RxViewHelper.getInstance().clicks(sortRight, aVoid ->
                {
                    clearSortState();
                    sortRight.setEnabled(false);
                    sortState = 0;
                    setMoneySort(sortState);
                    sortType = "time";
                    sortSort = "desc";
                    //获取接口
                    initData();
                    sortRight.setTextColor(getResources().getColor(R.color.color_eb2624));
                }

        );
        ImmersionBar.with(this).statusBarDarkFont(true).titleBar(goodsListTitle).statusBarColor(R.color.white).init();
        Bundle bundle = getIntent().getExtras();
        title = bundle.getString("goods_list_title", "未知标题");
        categoryId = bundle.getString("goods_list_id", null);
        Log.d("分类传过来的id", "initView: -----" + categoryId);
        goodsListTitle.setCenterText(title).setLeftImage(R.drawable.icon_back_black_2_0).setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {

            }
        });
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        goodsListRl.setLayoutManager(gridLayoutManager);
        goodsListRefresh.setRefreshHeader(new WaterDropHeader(this));
        goodsListRefresh.setEnableRefresh(false);
        goodsListRefresh.setEnableLoadmore(false);
        if (categoryId != null) {
            initData();
        }
    }

    private void initData() {
        NetClient.getInstance()
                .getShopService(ShopMainIF.class)
                .getGoodsListInfo(categoryId, sortType, sortSort)
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse<List<GoodsListEntity.DataEntity>>>() {
                    @Override
                    public void onSuccess(BaseResponse<List<GoodsListEntity.DataEntity>> response) {
                        dataEntityList = response.getData();
                        if (goodsListAdapter == null) {
                            goodsListAdapter = new GoodsListAdapter(dataEntityList, mContext);
                            goodsListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                                @Override
                                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                    switch (view.getId()) {
                                        case R.id.goods_list_cl:
//                                            if (MyApplication.getInstance().getCurrentUser() == null) {
//                                                BaseTool.goActivity(mContext, LoginActivity.class, null);
//                                                return;
//                                            }
                                            Bundle bundle = new Bundle();
                                            bundle.putString("goods_info_id", dataEntityList.get(position).getId());
                                            BaseTool.goActivity(mContext, GoodsInfoActivity.class, bundle);
                                            break;
                                    }
                                }
                            });
                            goodsListRl.setAdapter(goodsListAdapter);
                        } else {
                            goodsListAdapter.setNewData(dataEntityList);
                        }

                    }
                });
    }

    private void setMoneySort(int state) {
        switch (state) {
            case 0:
                sortImg.setImageResource(R.drawable.icon_sort_default_2_0);
                sortTxt.setTextColor(getResources().getColor(R.color.color_999999));
                sortType = "";
                sortSort = "";
                break;
            case 1:
                sortImg.setImageResource(R.drawable.icon_sort_up_2_0);
                sortTxt.setTextColor(getResources().getColor(R.color.color_eb2624));
                sortType = "price";
                sortSort = "asc";
                //获取接口
                initData();
                break;
            case 2:
                sortImg.setImageResource(R.drawable.icon_sort_down_2_0);
                sortTxt.setTextColor(getResources().getColor(R.color.color_eb2624));
                sortType = "price";
                sortSort = "desc";
                //获取接口
                initData();
                break;
        }
    }

    private void clearSortState() {
        sortLeft.setEnabled(true);
        sortRight.setEnabled(true);
        sortLeft.setTextColor(getResources().getColor(R.color.color_999999));
        sortRight.setTextColor(getResources().getColor(R.color.color_999999));
        sortTxt.setTextColor(getResources().getColor(R.color.color_999999));
    }
}
